from flask import request, json
from flask_cors import CORS
import requests
from model import *
from mq import mq_connect
import redis

app = create_app()
app.app_context().push()
db.create_all()
channel = mq_connect()
redis_conn = redis.Redis(host= 'redis', port = 6379, decode_responses=True)
CORS(app)

@app.route('/order', methods=["POST", "GET"])
def order():
    channel.basic_publish(exchange='', routing_key='hello', body=request.get_json())
    print(" [x] Sent 'Hello World!'")
    return {
            "status": 1,
            "message": "Submit successful and please wait for minutes..."
        }
    # data = request.get_json()
    # data = json.loads(data)
    # name = data['name']
    # studentid = data['studentID']
    # dorm = data['dorm']
    # unit = data['unit']
    # num = data['num']
    # infoname = data['infoname']
    # infostudentid = data['infostudentid']
      
    # filters = {
    #     Userinfo.name == name,
    #     Userinfo.studentid == studentid
    # }
    # userinfo = Userinfo.query.filter(*filters).first()
    # if(userinfo is None):
    #     return {
    #         "status": 0,
    #         "message": "Wrong user"
    #     }

    # dorm = Dorm.query.filter_by(dormid = dorm).first()
    # filters = {
    #     Unit.unitid == unit,
    #     Unit.dorm_id == dorm.id
    # }
    # unit = Unit.query.filter(*filters).first()
    # order = Order(userinfo, unit, userinfo.gender, num, False)
    # db.session.add(order)
    # db.session.commit()

    # num = int(num)
    # gender = userinfo.gender
    # if(num == 1) :
    #     filters = {
    #         Userinfo.name == infoname,
    #         Userinfo.studentid == infostudentid
    #     } 
    #     user = Userinfo.query.filter(*filters).first()
    #     if(user is None):
    #         return {
    #             "status": 0,
    #             "message": "The user does not exist"
    #         }
    #     if(Information.query.filter_by(userinfo_id = user.id).first() is not None):
    #         return {
    #             "status": 0,
    #             "message": "Repeat application"
    #         }
    #     orderinfo = Orderinfo(user, order)
    #     db.session.add(orderinfo)
    #     db.session.commit()
    # else:
    #     for i in range(num):
    #         name = infoname[i]
    #         studentid = infostudentid[i]
    #         filters = {
    #             Userinfo.name == name,
    #             Userinfo.studentid == studentid
    #         } 
    #         user = Userinfo.query.filter(*filters).first()
    #         if(user is None):
    #             return {
    #                 "status": 0,
    #                 "message": "The user does not exist"
    #             }
    #         if(user.gender != gender):
    #             return {
    #                 "status": 0,
    #                 "message": "Gender inconsistencies"
    #             }
    #         if(Information.query.filter_by(userinfo_id = user.id).first() is not None):
    #             return {
    #                 "status": 0,
    #                 "message": "Repeat application"
    #             }
    #         orderinfo = Orderinfo(user, order)
    #         db.session.add(orderinfo)
    #         db.session.commit()
        
    # rooms = Room.query.filter_by(unit_id = unit.id).all()
    # for room in rooms:
    #     if(room.rest >= num):
    #         order.status = True
    #         room.rest -= num
    #         if num == 1:
    #             filters = {
    #                 Userinfo.name == infoname,
    #                 Userinfo.studentid == infostudentid
    #             } 
    #             user = Userinfo.query.filter(*filters).first()
    #             info = Information(user, room)
    #             db.session.add(info)
    #             db.session.commit()
    #         else:
    #             for i in range(num):
    #                 name = infoname[i]
    #                 studentid = infostudentid[i]
    #                 filters = {
    #                     Userinfo.name == name,
    #                     Userinfo.studentid == studentid
    #                 } 
    #                 user = Userinfo.query.filter(*filters).first()
    #                 info = Information(user, room)
    #                 db.session.add(info)
    #                 db.session.commit()
                
            
    #         return {
    #             "status": 1,
    #             "message": "Successful",
    #             "room": room.roomid
    #         }
                
    # return {
    #     "status": 0,
    #     "message": "too many people"
    # }

    ##################################################
    # token = request.headers.get('Authorization')

    # if(token):
    #     Headers = {
    #         'Authorization': token
    #     }
    #     r = requests.get('http://jwt:5000/verify-token', headers=Headers)
    #     if(r.status_code == 200 and r.json().get("Valid")):
    
        


if __name__ == "__main__":
    app.run(host = '0.0.0.0')